package cn.lanqiao.ui;

import java.awt.EventQueue;

import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;

import cn.lanqiao.entity.Users;
import cn.lanqiao.service.UserService;
import cn.lanqiao.service.impl.UserServiceImpl;

import javax.swing.JPanel;
import java.awt.BorderLayout;
import javax.swing.border.LineBorder;
import java.awt.Color;
import java.awt.Font;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class ManUserFrm extends JInternalFrame {

	private static final long serialVersionUID = 4322820353065805795L;
	private JTable table;

	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					ManUserFrm frame = new ManUserFrm();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public ManUserFrm() {
		setTitle("账号管理");
		setClosable(true);
		setBounds(100, 100, 627, 443);

		JPanel panel = new JPanel();
		getContentPane().add(panel, BorderLayout.CENTER);
		panel.setLayout(null);

		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(5, 3, 601, 219);
		panel.add(scrollPane);

		table = new JTable();
		table.setFont(new Font("微软雅黑", Font.PLAIN, 20));
		table.setFillsViewportHeight(true);
		table.setRowHeight(30);
		// 设置表格数据居中
		DefaultTableCellRenderer cr = new DefaultTableCellRenderer();
		cr.setHorizontalAlignment(JLabel.CENTER);
		table.setDefaultRenderer(Object.class, cr);
		table.setBorder(new LineBorder(new Color(0, 0, 0)));
		cn.lanqiao.service.UserService users = new cn.lanqiao.service.impl.UserServiceImpl();
		table.setModel(new DefaultTableModel(users.getAllUsers(),
			new String[] {
				"\u8D26\u53F7", "\u5BC6\u7801", "\u7535\u8BDD\u53F7\u7801", "\u8EAB\u4EFD"
			}
		));
		scrollPane.setViewportView(table);

		JButton updateUser = new JButton("修改");
		updateUser.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				//修改
				// 判斷是否选择需要修改的行
				int count = table.getSelectedRowCount();
				if (count == 0) {
					JOptionPane.showMessageDialog(null, "请选择需要修改的数据！");
					return;
				}
				String account, password, telephone, role;
				int rowNum = table.getSelectedRow();// 获取行号
				// 获取单元格的值
				// 获取行中数据
				account = table.getValueAt(rowNum, 0).toString();
				password = table.getValueAt(rowNum, 1).toString();
				telephone = table.getValueAt(rowNum, 2).toString();
				role = table.getValueAt(rowNum, 3).toString();
				Users user = new Users(account, password, telephone, role);
				updateUserFrm updates =new updateUserFrm(user,table);
				updates.setVisible(true);
			}

		});
		updateUser.setFont(new Font("微软雅黑", Font.PLAIN, 20));
		updateUser.setBounds(14, 321, 118, 40);
		panel.add(updateUser);

		JButton addUser = new JButton("添加");
		addUser.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				//添加账号
				addUsersFrm addUser = new addUsersFrm(ManUserFrm.this);
				addUser.setVisible(true);
			}
		});
		addUser.setFont(new Font("微软雅黑", Font.PLAIN, 20));
		addUser.setBounds(157, 324, 129, 37);
		panel.add(addUser);

		JButton deleteUser = new JButton("删除");
		deleteUser.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				// 删除账号
				// 删前确认
				int count = table.getSelectedRowCount();
				if (count != 0) {
					int val = JOptionPane.showConfirmDialog(null, "确认删除吗？", "删除确认", JOptionPane.YES_NO_OPTION);
					if (val == 1) {// 取消
						return;
					}
				} else {
					JOptionPane.showMessageDialog(null, "请选择需要删除的数据！");
					return;
				}

				int rowNum = table.getSelectedRow();// 获取行号
				// 获取单元格的值
				String sid = table.getValueAt(rowNum, 0).toString();// 获取行中数据
				UserService remove = new UserServiceImpl();
				remove.deleteUser(sid);
				bindJTable();
				scrollPane.setViewportView(table);
			}
		});
		deleteUser.setFont(new Font("微软雅黑", Font.PLAIN, 20));
		deleteUser.setBounds(314, 324, 118, 34);
		panel.add(deleteUser);
	}
	public void bindJTable() {
		cn.lanqiao.service.UserService users = new cn.lanqiao.service.impl.UserServiceImpl();
		table.setModel(new DefaultTableModel(users.getAllUsers(), new String[] { "账号", "密码", "电话号码", "身份" }));
	}
}
